<!--  -->
<template>
  <div id="main">
    <h2>分润设置</h2>

    <el-button type="primary" style="margin:20px" @click="handleOpen('添加返佣比例',1)">添加返佣比例</el-button>

    <!-- <div id="search-card">
      <div id="search-content">
        <el-form v-model="search" :inline="true">

          <el-form-item label="昵称">
            <el-input v-model="search.username" />
          </el-form-item>

          <el-form-item label="所属机构">
            <el-select v-model="search.orgId" placeholder="请选择">
              <el-option
                v-for="o in org"
                :key="o.orgId"
                :value="o.orgId"
                :label="o.orgName"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="用户角色">
            <el-select v-model="search.userGroup" placeholder="请选择">
              <el-option key="1" label="系统管理员" value="1" />
              <el-option key="2" label="代理人" value="2" />
              <el-option key="3" label="机构管理员" value="3" />
              <el-option key="4" label="分支机构管理员" value="4" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="conditionQuery">查找</el-button>
            <el-button icon="el-icon-refresh" circle @click="fetchData()" />
          </el-form-item>

        </el-form>
      </div>

    </div> -->

    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >

        <el-table-column
          label="序号"
          type="index"
          width="80"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="rebateLevelId"
          label="返利等级"
        />

        <el-table-column
          align="center"
          prop="rebateLevelRatio"
          label="个人返利比例"
        >
          <template slot-scope="Scope">
            <span v-if="Scope.row.rebateLevelTeam==1">无</span>
            <span v-else>{{ Scope.row.rebateLevelRatio }}%</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="rebateTeamRatio"
          label="团队返利比例"
        >
          <template slot-scope="Scope">
            <span>{{ Scope.row.rebateTeamRatio }}%</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="rebateWxpay"
          label="微信支付比例"
        >
          <template slot-scope="Scope">
            <span v-if="Scope.row.rebateLevelTeam==1">无</span>
            <span v-else>{{ Scope.row.rebateWxpay }}%</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="rebateKjpay"
          label="快捷支付比例"
        >
          <template slot-scope="Scope">
            <span v-if="Scope.row.rebateLevelTeam==1">无</span>
            <span v-else>{{ Scope.row.rebateKjpay }}%</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="rebateLevelRatio"
          label="总交易量单位"
        >
          <template slot-scope="Scope">
            <span>{{ Scope.row.totalTradingVolume }}元</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="rebateLevelTeam"
          label="返利类型"
        >
          <template slot-scope="Scope">
            <span v-if="Scope.row.rebateLevelTeam==0">个人</span>
            <span v-if="Scope.row.rebateLevelTeam==1">团队</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleOpen('修改返利比例',2,Scope.row.rebateLevelId)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="handleDelete(Scope.row.rebateLevelId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog title="修改返佣比例" :close-on-click-modal="false" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form ref="rebateLevel" :model="rebateLevel" label-position="top" :rules="rules">

        <el-form-item prop="rebateLevelId" label="返佣等级" :label-width="formLabelWidth">
          <el-input v-model="rebateLevel.rebateLevelId" autocomplete="off" :readonly="readonly" />
        </el-form-item>

        <el-form-item prop="rebateLevelTeam" label="返利类型" :label-width="formLabelWidth">
          <el-select v-model="rebateLevel.rebateLevelTeam" placeholder="请选择">
            <el-option key="0" label="个人" value="0" />
            <el-option key="1" label="团队" value="1" />
          </el-select>
        </el-form-item>

        <el-form-item v-if="rebateLevel.rebateLevelTeam==0" prop="rebateLevelRatio" label="个人返佣比例" :label-width="formLabelWidth">
          <el-input v-model="rebateLevel.rebateLevelRatio" autocomplete="off">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>

        <el-form-item v-if="rebateLevel.rebateLevelTeam==0" prop="rebateWxpay" label="微信支付比例" :label-width="formLabelWidth">
          <el-input v-model="rebateLevel.rebateWxpay" autocomplete="off">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>

        <el-form-item v-if="rebateLevel.rebateLevelTeam==0" prop="rebateKjpay" label="快捷支付比例" :label-width="formLabelWidth">
          <el-input v-model="rebateLevel.rebateKjpay" autocomplete="off">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>

        <el-form-item prop="rebateTeamRatio" label="团队返佣比例" :label-width="formLabelWidth">
          <el-input v-model="rebateLevel.rebateTeamRatio" autocomplete="off">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>

        <el-form-item prop="totalTradingVolume" label="总交易量单位" :label-width="formLabelWidth">
          <el-input v-model="rebateLevel.totalTradingVolume" autocomplete="off">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">取 消</el-button>
        <el-button v-if="editType==2" type="primary" @click="handleUpdate('rebateLevel')">修 改</el-button>
        <el-button v-if="editType==1" type="primary" @click="handleAddUser('rebateLevel')">添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { getAllRebateLevel, getRebateLevelById, updateRebateLevel, addRebateLevel, deleteRebateLevel } from '@/api/rebateLevel'
export default {
// import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      listLoading: false,
      editType: 1, // 1 添加 2 修改
      rebateLevel: {},
      dialogFormVisible: false,
      formLabelWidth: '200px',
      readonly: false,
      rules: {
        rebateLevelId: [{ required: true, message: '返佣等级不能为空', trigger: 'blur' }],
        rebateLevelRatio: [{ required: true, message: '个人返佣比例不能为空', trigger: 'blur' }],
        rebateLevelTeam: [{ required: true, message: '团队返佣比例不能为空', trigger: 'blur' }],
        rebateTeamRatio: [{ required: true, message: '返利类型不能为空', trigger: 'blur' }],
        totalTradingVolume: [{ required: true, message: '总交易量单位不能为空', trigger: 'blur' }],
        rebateWxpay: [{ required: true, message: '微信支付比例', trigger: 'blur' }],
        rebateKjpay: [{ required: true, message: '快捷支付比例', trigger: 'blur' }]
      }
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.listLoading = true
      getAllRebateLevel().then((resp) => {
        this.list = resp.obj
        this.listLoading = false
      })
    },
    handleDelete(id) {
      this.$confirm('确认删除等级' + id + '？').then(() => deleteRebateLevel(id).then((data) => {
        this.$notify({
          title: '操作成功',
          type: 'success'
        })
        const totalPage = Math.ceil((this.list.length - 1) / this.pagesize)
        const page = this.currentPage > totalPage ? totalPage : this.currentPage
        this.currentPage = this.currentPage < 1 ? 1 : page
        this.fetchData()
      }))
    },
    handleOpen(title, type, id) {
      this.readonly = false
      this.rebateLevel = {}
      this.dialogFormVisible = true
      this.title = title
      this.editType = type
      if (type === 2) {
        this.readonly = true
        getRebateLevelById(id).then((resp) => {
          this.rebateLevel = resp.obj
          this.rebateLevel.rebateLevelTeam = String(this.rebateLevel.rebateLevelTeam)
        })
      }
    },
    handleClose() {
      this.dialogFormVisible = false
      this.rebateLevel = {}
    },
    handleAddUser(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addRebateLevel(this.rebateLevel.rebateLevelId, this.rebateLevel.rebateLevelRatio, this.rebateLevel.rebateLevelTeam, this.rebateLevel.rebateTeamRatio, this.rebateLevel.totalTradingVolume, this.rebateLevel.rebateWxpay, this.rebateLevel.rebateKjpay).then((resp) => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.rebateLevel = {}
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleUpdate(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateRebateLevel(this.rebateLevel.rebateLevelId, this.rebateLevel.rebateLevelRatio, this.rebateLevel.rebateLevelTeam, this.rebateLevel.rebateTeamRatio, this.rebateLevel.totalTradingVolume, this.rebateLevel.rebateWxpay, this.rebateLevel.rebateKjpay).then((resp) => {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    }
  } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped>
#main{
  margin-left: 5px;
  margin-right: 5px;
}
#table-area{
  margin-left: 10px;
  margin-right: 10px;
}
#search-card {
  line-height: 0px;
  margin-left: 10px;
  text-align: left;
  width: 100%;
  height: 70px;
  overflow-x: hidden;
}
</style>
